package com.csii.pos.uofplan;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.adhoc.util.serializer.PeSerializerUtils;
import com.csii.pos.caacctewsu.CaAcctEwSu;
import com.csii.pos.cashdaily.CashCurBlc;
import com.csii.pos.cashdaily.service.CashCurBlcService;
import com.csii.pos.rpc.CashRequest;
import com.csii.pos.rpc.CashResponse;
import com.csii.pos.rpc.upp.UppCashClient;
import com.csii.pos.rpc.vo.CashRequestVo;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @description: 余额实时统计
 * @author: liuzhonghua
 * @create: 20210830
 */
@RestController
@RequestMapping("cash")
public class CashController {
    @Autowired
    private UppCashClient cashClient;
    @Autowired
    private CashCurBlcService cashCurBlcService;
/**
 * @description: 调用接口并将数据存到数据库
 * @param: cashRequest
 * @return: cashResponses
 * @author liuzhonghua
 * @date:  20210914
 */
    @RequestMapping("queryCashCurBlc")
    public APIResult<?> queryCashCurBlc(@RequestBody CashRequestVo cashRequestVo){
        CashRequest cashRequest = ConvertUtils.convert(CashRequest.class, cashRequestVo);
//        String[] strs = {"com.csii.upp.api.query.vo.UppCashReq"};
//        cashRequest.setService("com.csii.upp.api.query.service.UppCashService");
//        cashRequest.setMethod("query");
//        cashRequest.setArgTypes(strs);
//        try {
//            cashRequest.setArgValues(PeSerializerUtils.serialize(cashRequest));
//        }catch (Exception e){
//            throw new ParamValidityException("数据加密失败");
//        }
//        List<CashResponse> cashResponses = cashClient.queryCashCurBlc(cashRequest);
        List<CashResponse> cashResponses = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CashResponse cashResponse = new CashResponse();
            cashResponse.setCshCurBlc(544.1);
            cashResponse.setCshItemCd("工商");
            cashResponse.setCshItemName("545646");
            cashResponse.setStatus("0");
            cashResponse.setFinished("1");
            cashResponse.setCshRtpTs("20210914");
            cashResponses.add(cashResponse);
        }
        Page<CashCurBlc> page = new Page<>(cashRequestVo.getPageNo(), cashRequestVo.getPageSize());
        IPage<CashCurBlc> cashCurBlcIPage = cashCurBlcService.saveCashCurBlc(page, cashResponses);
        return APIResult.successResult(PageVo.getPageVoFromIPage(cashCurBlcIPage));
    }
    @RequestMapping("pageCashCurBlc")
    public APIResult<?> pageCashCurBlc(@RequestBody CashRequestVo cashRequestVo) {
        CashCurBlc cashCurBlc = ConvertUtils.convert(CashCurBlc.class, cashRequestVo);
        Page<CashCurBlc> page = new Page<>(cashRequestVo.getPageNo(), cashRequestVo.getPageSize());
        IPage<CashCurBlc> cashCurBlcIPage = cashCurBlcService.pageCashCurBlc(page,cashCurBlc);
        return APIResult.successResult(PageVo.getPageVoFromIPage(cashCurBlcIPage));
    }
}
